/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SplashScreen.java
 *
 * Created on Nov 21, 2011, 11:04:08 AM
 */

package penny.downloadmanager.view;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import penny.downloadmanager.model.Model;
import penny.downloadmanager.model.gui.SplashScreenModel;

/**
 *
 * @author john
 */
public class SplashScreen extends javax.swing.JDialog implements PropertyChangeListener {

    /** Creates new form SplashScreen */
    public SplashScreen() {
        this.setUndecorated(true);
        initComponents();
        this.setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        progress = new javax.swing.JProgressBar();
        message = new javax.swing.JLabel();
        stage = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setResizable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(progress, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
            .addComponent(message, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
            .addComponent(stage, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(173, Short.MAX_VALUE)
                .addComponent(stage, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(message, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(progress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel message;
    private javax.swing.JProgressBar progress;
    private javax.swing.JLabel stage;
    // End of variables declaration//GEN-END:variables

    public void propertyChange(PropertyChangeEvent evt) {
        if(evt.getPropertyName().equals(SplashScreenModel.PROP_VISIBLE)) {
            this.setVisible((Boolean)evt.getNewValue());
        } else if(evt.getPropertyName().equals(SplashScreenModel.PROP_STAGE)) {
            progress.setIndeterminate(true);
            progress.setStringPainted(false);
            stage.setText(evt.getNewValue().toString());
        } else if(evt.getPropertyName().equals(SplashScreenModel.PROP_MESSAGE)) {
            message.setText(evt.getNewValue().toString());
        } else if(evt.getPropertyName().equals(SplashScreenModel.PROP_CURRENT)) {
            progress.setValue((Integer) evt.getNewValue());
            progress.setString(String.format("%1$.2f%%", (Model.getSplashScreenModel().getCurrent() / (double) Model.getSplashScreenModel().getSize()) * 100));
        } else if(evt.getPropertyName().equals(SplashScreenModel.PROP_SIZE)) {
            progress.setIndeterminate(false);
            progress.setStringPainted(true);
            progress.setMaximum((Integer) evt.getNewValue());
            progress.setString(String.format("%1$.2f%%", (Model.getSplashScreenModel().getCurrent() / (double) Model.getSplashScreenModel().getSize()) * 100));
        }
    }

}
